package tony.crypto.digest

import org.bouncycastle.crypto.digests.SM3Digest
import tony.core.codec.Encoding
import tony.core.utils.Digest

/**
 * Sm3 is
 * @author tangli
 * @date 2025/10/06 11:46
 */
public object Sm3 : Digest {
    override fun digest(src: CharSequence): String {
        val digest = SM3Digest()
        val input = src.toString().toByteArray(Charsets.UTF_8)
        digest.update(input, 0, input.size)
        val result = ByteArray(digest.digestSize)
        digest.doFinal(result, 0)
        return Encoding.HEX.encodeToString(result)
    }
}

/**
 * 字符串转为SM3摘要
 * @return [String]
 * @author tangli
 * @date 2024/01/22 15:30
 */
public fun CharSequence.sm3(): String =
    Sm3.digest(this)
